import threading

import schedule

from application.logging import logger
from application.task.task_data_process_instant import TaskDataProcessInstant
from application.task.task_data_process_accum import TaskDataProcessAccum
from application.task.task_outside_temperature import TaskOutsideTemperature

#
# 定时任务：室外温度
schedule.every(interval=10).minutes.do(TaskOutsideTemperature.task)
#
# 定时任务：数据处理-累热
schedule.every(interval=30).minutes.do(TaskDataProcessAccum.task)
#
# 定时任务：数据处理-瞬热
schedule.every(interval=40).minutes.do(TaskDataProcessInstant.task)


#
def task_thread_target():
    # while ->
    while True:
        schedule.run_pending()
        pass  # <- while
    pass  # <- task_thread_target


pass


#
def task_thread_start():
    # 打印日志
    logger.info(f"定时任务线程:启动:开始 {'*' * 100}")
    #
    # https://docs.python.org/3/library/threading.html#threading.Thread.daemon
    #
    # daemon
    # A boolean value indicating whether
    # this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise
    # RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon
    # thread and therefore all threads created in the main thread default to daemon = False.
    #
    # The entire Python program exits when no alive non-daemon threads are left.
    # daemon 默认值为False, 当主进程停止时, 线程不会自动退出, 因此需要将 daemon 参数设置成True

    # 创建线程
    thread_task = threading.Thread(target=task_thread_target, daemon=True)
    # 启动线程
    thread_task.start()
    # 线程状态:isDaemon()
    print("thread_task.isDaemon()", thread_task.isDaemon())
    print("thread_task.is_alive()", thread_task.is_alive())
    # 打印日志
    logger.info(f"定时任务线程:启动:完成 {'*' * 100}")
    pass  # <-task_thread_target


pass
